package com.mgvlife.ddlw.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.mgvlife.ddlw.entity.Clazz;
import com.mgvlife.ddlw.entity.Schedule;
import com.mgvlife.ddlw.entity.SignInInfo;
import com.mgvlife.ddlw.entity.Student;
import com.mgvlife.ddlw.formbean.ClazzForm;
import com.mgvlife.ddlw.service.ClazzService;
import com.mgvlife.ddlw.vo.Result;
import com.mgvlife.ddlw.vo.VoClazz;
import com.mgvlife.ddlw.vo.VoClazz2;
import com.xinhuo.dao.DaoSupport;
import com.xinhuo.util.QueryResult;
import com.xinhuo.vo.JsonEasyUI;

@Service("clazzService")
public class ClazzServiceImpl extends DaoSupport implements ClazzService {

	public Result clazzAdd(ClazzForm dataForm) throws Exception {
		String grade = dataForm.getGrade(), major = dataForm.getMajor(), clazzNum = dataForm
				.getClazzNum();
		boolean isClazzExist = this.checkClazzExist(grade, major, clazzNum);
		if (isClazzExist) {
			return new Result("error", "班级已存在");
		}
		Clazz clazz = new Clazz();
		clazz.setId(UUID.randomUUID().toString());
		clazz.setGrade(grade);
		clazz.setMajor(major);
		clazz.setClazzNum(clazzNum);
		List<Student> students = new ArrayList<Student>();
		clazz.setStudents(students);
		clazz.setSchedules(new ArrayList<Schedule>());
		this.save(clazz);
		return Result.success();
	}

	public Result clazzUpdate(ClazzForm dataForm) throws Exception {
		String clazzId = dataForm.getId();
		Clazz clazz = null;
		if (clazzId == null || clazzId.trim().equals("")) {
			return new Result("error", "无此班级");
		} else {
			clazz = this.findById(Clazz.class, clazzId);
		}
		String grade = dataForm.getGrade(), major = dataForm.getMajor(), clazzNum = dataForm
				.getClazzNum();
		if (!grade.equals(clazz.getGrade()) || !major.equals(clazz.getMajor())
				|| !clazzNum.equals(clazz.getClazzNum())) {
			boolean isClazzExist = this.checkClazzExist(grade, major, clazzNum);
			if (isClazzExist) {
				return new Result("error", "班级已存在");
			}
			clazz.setGrade(grade);
			clazz.setMajor(major);
			clazz.setClazzNum(clazzNum);
		}
		this.update(clazz);
		return Result.success();
	}

	public JsonEasyUI<VoClazz> getData(ClazzForm dataForm) {
		JsonEasyUI<VoClazz> result = new JsonEasyUI<VoClazz>();
		QueryResult<Clazz> qr = this.getScrollData(Clazz.class, null, null,
				dataForm.getSearchTitle(), dataForm.getSearch(),
				dataForm.getPage(), dataForm.getRows(), dataForm.getSort(),
				dataForm.getOrder());
		result.setTotal(qr.getTotalrecord());
		List<VoClazz> clazz_l = new ArrayList<VoClazz>();
		for (Clazz clazz : qr.getResultList()) {
			clazz_l.add(new VoClazz(clazz));
		}
		result.setRows(clazz_l);
		return result;
	}

	public JsonEasyUI<VoClazz2> getAllData() {
		JsonEasyUI<VoClazz2> result = new JsonEasyUI<VoClazz2>();
		LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
		orderBy.put("grade", "asc");
		orderBy.put("major", "asc");
		orderBy.put("clazzNum", "asc");
		List<Clazz> clazzs = this.findAll(Clazz.class, null, null, orderBy);
		List<VoClazz2> clazz_l = new ArrayList<VoClazz2>();
		for (Clazz clazz : clazzs) {
			clazz_l.add(new VoClazz2(clazz));
		}
		result.setRows(clazz_l);
		result.setTotal((long) clazzs.size());
		return result;
	}

	public Result clazzDelete(ClazzForm dataForm) throws Exception {
		String clazzId = dataForm.getId();
		if (clazzId != null && clazzId.length() > 0) {
			String whereSQL = "";
			String[] ids = clazzId.split(";");
			boolean isFirst = true;
			for (int i = 0; i < ids.length; i++) {
				if (isFirst) {
					whereSQL = "id in (?";
					isFirst = false;
				} else {
					whereSQL += ", ?";
				}
			}
			whereSQL += ")";
			List<Clazz> clazzs = this.findAll(Clazz.class, whereSQL, ids);
			for (Clazz clazz : clazzs) {
				for (Schedule schedule : clazz.getSchedules()){
					schedule.getClazzs().remove(clazz);
					schedule.setSignInInfos(null);
				}
				this.delete(clazz);
			}
		} else {
			return new Result("error", "班级不存在");
		}
		return Result.success();
	}

	public boolean checkClazzExist(String grade, String major, String clazzNum) {
		String whereSQL = "grade=? and major=? and clazznum=?";
		Object[] whereParams = { grade, major, clazzNum };
		long count = this.getDataCount(Clazz.class, whereSQL, whereParams);
		return count > 0;
	}

}
